<template>
    <div style="min-width:809px">
        <div v-if="$route.path == '/technicianHistory'">
            <div class="vs-search-box">
                <el-form ref="form" :inline="true" :model="paging" label-width="120px">
                    <el-form-item label="门店：">
                        <el-select v-model="paging.store_id" @change="getShiftType" style="width:250px" placeholder="请选择">
                            <el-option
                                v-for="item in shopList"
                                :key="item.id"
                                :label="item.title"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                     <el-form-item label="技师姓名：">
                        <el-input style="width:250px" clearable v-model="paging.name"></el-input>
                    </el-form-item>
                    <el-form-item label="" class="vs-right-btns">
                        <el-button @click="reset">重置</el-button>
                        <el-button @click="handleCurrentChange(1)" type="primary">查询</el-button>
                    </el-form-item>
                    <el-form-item label="技师工号：">
                        <el-input style="width:250px" clearable v-model="paging.number"></el-input>
                    </el-form-item>

                    <el-form-item label="日期：">
                        <el-date-picker
                        @change="setDate"
                        v-model="value1"
                        value-format="yyyy-MM-dd"
                        style="width:250px"
                        type="daterange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期">
                        </el-date-picker>
                    </el-form-item>
                </el-form>
            </div>
            <div class="vs-table-box">
                <div class="operation-box clear-float">
                    <div class="title-left"> 
                    </div>
                    <!-- <el-button icon="el-icon-plus" @click="addTech"  type="primary">新增</el-button> -->
                    <el-button style="margin-left:auto;" @click="derive" :loading="isDerive" icon="el-icon-download" size="medium" plain>导出</el-button>
                </div>
                <el-table v-loading="loading" :data="tableData" header-row-class-name="has-gutter-table" style="width: 100%">
                    <el-table-column prop="date" label="日期" ></el-table-column>
                    <el-table-column prop="store.title" label="门店" > </el-table-column>
                    <el-table-column prop="staff_name" label="技师姓名" > </el-table-column>
                    <el-table-column prop="staff_number" label="工号" > </el-table-column>
                     <el-table-column prop="id" label="技师国籍" >
                        <template slot-scope="scope">
                            <span >{{scope.row.staff_nationality==1?'中国':'泰国'}}</span>
                        </template>
                     </el-table-column>
                    <el-table-column prop="sort" label="今日排序" > </el-table-column>
                    <el-table-column prop="upper_clock_number" label="今日上钟次数" ></el-table-column>
                    <!-- <el-table-column prop="create_time" label="累计上架" > </el-table-column> -->
                    <el-table-column prop="spot_clock_number" label="今日点钟次数"> </el-table-column>
                    <el-table-column prop="today_upper_clock_time" label="今日上钟时长"> 
                    </el-table-column>
                </el-table>
                <div class="vs-paging-box">
                    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="paging.page" :page-size="paging.limit" layout="sizes,prev, pager, next, jumper" :total="paging.total">
                    </el-pagination>
                </div>
            </div>
        </div>
        <router-view></router-view>
        <el-dialog title="新增技师上钟" :visible.sync="dialogVisible" :before-close="handleClose">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="技师姓名" prop="staff_id">
                    <el-select v-model="ruleForm.staff_id" @change="chng" placeholder="请选择">
                        <el-option
                            v-for="item in staffList"
                            :key="item.id"
                            :label="item.name"
                            :disabled="item.disabled"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="工号" prop="number">
                    <el-input v-model="ruleForm.number" readonly placeholder=""></el-input>
                </el-form-item>
                <el-form-item label="排钟类型" prop="type">
                    <el-select v-model="ruleForm.type" placeholder="请选择">
                        <el-option label="轮钟" :value="1"> </el-option>
                        <el-option label="点钟" :value="2"> </el-option>
                        <el-option label="加班1点钟" :value="3"> </el-option>
                        <el-option label="加班1轮钟" :value="4"> </el-option>
                        <el-option label="加班2点钟" :value="5"> </el-option>
                        <el-option label="加班2轮钟" :value="6"> </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="备注" prop="note">
                    <el-input v-model="ruleForm.note"  type="textarea" :rows="4" placeholder=""></el-input>
                </el-form-item>
                <el-form-item label="顺序" prop="sort">
                     <el-input-number v-model="ruleForm.sort" :step="1" step-strictly :min="0" size="mini"></el-input-number>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="handleClose()">取 消</el-button>
                <el-button :loading="isBtn" type="primary" @click="rowClockSave">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            value1:'',
            recordList: [],
            loading: false,
            loading2: false,
            isBtn: false,
            isBtn2: false,
            isInfo: false,
            dialogVisible: false,
            shiftTypeList:[],
            staffList:[],
            paging: {
                store_id: '',//	门店ID
                name:'',	//姓名
                number:'',//	技师工号
                start_time:'',//国籍
                end_time:'',//	班次
                page: 1,
                total: 0,
                limit: 10,
            },
            rules:{
                store_id:{ required: true, message: '请选择门店', trigger: 'change' },
                shift_id:{ required: true, message: '请选择班次', trigger: 'change' },
                staff_id:{ required: true, message: '请选择技师', trigger: 'change' },
                type:{ required: true, message: '请选择排钟类型', trigger: 'change' },
            },
            ruleForm: {
                store_id:'',//门店ID
                staff_id:'',//员工ID
                shift_id:'',//班次ID
                type:'',//排钟类型
                note:'',//备注
                sort:'',//排序
            },
            classList:[],
            shopList:[],
            isDerive:false,
            tableData: []
        };
    },
    filters:{
        typeName(val){
            switch (val) {
                case 0:
                    val = '下架'
                    break;
                case 1:
                    val = '正常'
                    break;
            }
            return val;
        }
    },
    created() {
        if (this.$route.path == '/technicianHistory') {
            // this.handleCurrentChange(1);
            this.homegetStore();
        }
    },
    methods: {
        async derive(){
            this.isDerive = true;
            let res = await this.$Http.historyExport(this.paging);
            this.utils.downloadXlsx(res,'技师历史排序')
            this.isDerive = false;
        },
        setDate(e){
            console.log(e);
            if(e&&e.length){
                this.paging.start_time = e[0];
                this.paging.end_time = e[1]
            }else{
                this.paging.start_time = '';
                this.paging.end_time = ''
            }
        },
        chng(e){
            this.ruleForm.number = this.staffList.find(v=>v.id == e).number;
        },
        addTech(){
            this.dialogVisible = true;
            this.ruleForm.store_id = this.paging.store_id;
            this.ruleForm.shift_id = this.paging.shift_id;
        },
        rowClockSave(){
            this.$refs['ruleForm'].validate(async (valid) => {
                if (!valid) return;
                this.isBtn = true;
                let obj = Object.assign({},this.ruleForm);
                let {code,data} = await this.$Http.rowClockSave(obj);
                this.isBtn = false;
                if(code){
                    this.$message.success('新增成功!');
                    this.handleClose();
                    this.handleCurrentChange(1);
                }
            });
        },
        async homegetStaff(){
            let {code,data} = await this.$Http.homegetStaff({
                store_id:this.paging.store_id
            });
            if(code){
                // data.forEach(v=>{
                //     for(let val of this.tableData){
                //         if(val.staff.id == v.id)
                //             v.disabled = true;
                //     }
                // })
                this.staffList = data;
            }
        },
        async setRowClock(){
            this.isBtn2 = true;
            let {code,data} = await this.$Http.setRowClock({
                store_id:this.paging.store_id,
                shift_id:this.paging.shift_id,
            });
            this.isBtn2 = false;
            if(code){
                this.handleCurrentChange(1);
                this.$message.success('技师排钟生成成功!')
            }
        },
        async getShiftType(store_id){//获取班次
            this.loading = true;
            this.homegetStaff()
            let {code,data} = await this.$Http.getStoreShift({store_id})
            if(code){
                this.paging.shift_id = data.length?data[0].id:''
                this.shiftTypeList = data;
                this.handleCurrentChange(1)
            }
        },
        async homegetStore(){//获取门店
            this.loading = true;
            let { code,data } = await this.$Http.homegetStore();
            if(code){
                this.paging.store_id = data.length?data[0].id:''
                this.shopList = data;
                // this.staffshift(data[0].id)
                // await this.getShiftType(data[0].id);
                this.handleCurrentChange();
            }
        },
        async setRow(type, row) {
            if(type==1){
                this.utils.goUrl(`/goodsInfo?title=详情&id=${row.id}`);
            }else if(type==3){
                this.utils.goUrl(`/goodsInfo?title=修改&id=${row.id}`);
            }else{
                if(this.isBtn)return;
                this.isBtn = true;
                let {code,data} = await this.$Http.changeGoods({
                    field:'status',
                    value:row.status==1?0:1,
                    id:row.id
                })
                this.isBtn = false;
                if(code){
                    this.$message.success(row.status==1?'下架成功':'上架成功');
                    // this.handleCurrentChange(1);
                    row.status=row.status==1?0:1;
                }
            }
            return;
        },
        handleClose(dom) {
            this.$refs['ruleForm'].resetFields();
            this.dialogVisible = false;
            dom && dom();
        },
        async repairGetList() {
            this.loading = true;
            let { code, data } = await this.$Http.history(this.paging);
            this.loading = false;
            if (code) {
                // data.data.forEach((ele,index)=>{
                //     if(ele.picture){
                //         let img = ele.picture.split(',')
                //         ele.picture = img
                //     }
                // })
                this.tableData = data.data;
                this.paging.total = data.total;
            }
        },
        reset() {//重置
            this.value1 = '';
            this.paging = Object.assign(this.paging,{name:'',number:'',end_time:'',start_time:''})
            this.handleCurrentChange(1)
        },
        handleSizeChange(val) {
            this.paging.limit = val;
            this.repairGetList()
        },
        handleCurrentChange(val) {
            this.paging.page = val;
            this.repairGetList()
        }
    },
};
</script>

<style lang="scss" scoped>
.image-thumbnail {
    width: 130px;
    height: 130px;
}
</style>